<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      width: 200px;
      height: 200px;
      border: 1px solid #000;
    }
  </style>
</head>
<body>
  <input type="text" placeholder="请输入你的内容"">
  <button>发送请求</button>
  <div>
  </div>

  <script>
    let inp = document.querySelector('input')
    let btn = document.querySelector('button')
    let div = document.querySelector('div')
    // websocket在浏览器的使用
    let socket = new WebSocket('ws://localhost:3000')

    socket.addEventListener('open', function() {
      div.innerHTML = '连接服务器成功'
    })

    btn.addEventListener('click', function() {
      let value = inp.value
      socket.send(value)
    })

    socket.addEventListener('message', function(e) {
      console.log(e.data);
      div.innerHTML = e.data
    })

    socket.addEventListener('close', function() {
      div.innerHTML = '服务已断开'
    })
  </script>
</body>
</html>